/**
 * Created by zhaoyanzhi on 16-8-26.
 */
var goodsInfo=[];
var goodsName=[];
angular.module('myApp')
    .service('cartSumCount',[function(){
       return{
           computeCartCount:function(){
               var cartCount=localStorage.getItem("cartCount") || 0
               cartCount++;
               localStorage.setItem("cartCount",cartCount);
        }}
    }])
    .service('itemName',[function(product,goodsName){
        return{
            saveGoodsName:function(product,goodsName){
                var index=goodsName.indexOf(product.name);
                if(index==-1){
                    goodsName.push(product.name)
                    localStorage.setItem("goodsName",goodsName)
                }else{
                    return index
                }

            }
        }
    }])
    .service('addCartCount',[function(product,index,goodsInfo){
        return{
            addCartCount:function(product,index,goodsInfo){
                if(index>=0){
                    var count=goodsInfo[index].count;
                    count++;
                }else{
                    count=1
                }
                return count
            }
        }
    }])
    .service('computePromotionCount',[function(count,product){
        return{
            computePromotionCount:function(count,product){
                var promotionCount=(product.preferential=="true")?parseInt(count/3):0;
                return  promotionCount
            }
        }
    }])
    .service('computeSubtotal',[function(count,promotionCount,product){
        return{
            computeSubtotal:function(count,promotionCount,product){
                var actuallyPaid=product.price*(count-promotionCount)
                var list=product.price*count
                var subtotal=(promotionCount)?(actuallyPaid+"元(原价:"+list+"元)"):(list+"元")
                return subtotal
            }
        }
    }])
    .service('saveProductInfo',[function(index,count, promotionCount,subtotal,product,goodsInfo) {

        return {
            saveProductInfo: function (index, count, promotionCount, subtotal, product,goodsInfo) {
                if (index >= 0) {
                    goodsInfo[index].count = count;
                    goodsInfo[index].promotionCount = promotionCount;
                    goodsInfo[index].subtotal = subtotal;
                } else {
                    product.count = count;
                    product.promotionCount = promotionCount;
                    product.subtotal = subtotal;
                    goodsInfo.push(product)
                }
                localStorage.setItem("goodsInfo", JSON.stringify(goodsInfo))
            }
        }
    } ])





